// pages/count/count.js
const app = getApp()
var request = require('../../utils/request.js');
var wxCharts = require('../../utils/wxcharts-min.js');   //引入wxChart文件
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    subjects:[
      {
        "id":1,
        "subject":"你是GG还是MM",
        "subject_type":1,
        "answer_option":[
          {
            "id": 0,
            "option": "GG",
            "count":"20",
            "proportion":"60"
          },
          {
            "id": 1,
            "option": "MM",
            "count": "10",
            "proportion": "40"
          }
        ]
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    console.log(options);
      console.log("统计id："+id);
      this.setData({
        id:id
      });
    this.getSubjects(id);

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取所有的问卷类型
  getSubjects: function (id) {
  
    let that = this;
    request.getData(
      "?type=countData&id="+id,
      (data) => {
        console.log(data);
        that.setData({
          subjects: data.data.subjects
        });

        let d = data.data.subjects;
        for(let i=0;i<d.length;i++){

          let a = d[i]["answer_option"];
          let s = [];
          if(a){
            let c = 100;
            for(let j=0;j<a.length;j++){
              s.push({ name: a[j]["option"], data: a[j]["proportion"]});
              c = c - a[j]["proportion"];
            }
            s.push({name:"未答题",data:c});
          }
          //图
          new wxCharts({
            canvasId: 'canvas'+i,
            type: 'pie',
            series: s,
            width: 300,
            height: 400,
            dataLabel: true,
          });
        }
       
      },
      (error) => {
          console.log(error);
      }
    );
  },
})